package com.example.myapplication;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import com.google.gson.Gson;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;


public class MainActivity extends AppCompatActivity {

    String path="https://www.wanandroid.com/user/login";

    public Button button_d;
    public  Button button_z;
    String username;
    String password;
    EditText et_username, et_password;
    Intent intent;
    AlertDialog dialog;
    private Handler handler=new Handler(){
        public void handleMessage(android.os.Message msg){

            if(msg.what==1){
                int  a =(Integer) msg.obj;
                startActivity(intent);

            }else{
                String ss=(String)msg.obj;
                dialog.setTitle("消息提示框");
                dialog.setIcon(R.mipmap.one);
                dialog.setMessage(ss);
                dialog.show();

            }
        }

    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button button_d =(Button) findViewById(R.id.login);
        Button button_a =(Button) findViewById(R.id.button);
        et_username  =(EditText)findViewById(R.id.username);
        et_password  =(EditText)findViewById(R.id.pwd);
        intent = new Intent(this,Appmain.class);
        dialog=new AlertDialog.Builder(this).create();
    }
    public void click1(View v){

        username = et_username.getText().toString().trim();
        password = et_password.getText().toString().trim();
        new Thread(){
            private HttpURLConnection coon;
            @Override
            public void run() {


                try {
                    URL url=new URL(path);
                    coon=(HttpURLConnection)url.openConnection();
                    coon.setRequestMethod("POST");
                    coon.setReadTimeout(5000);
                    coon.setConnectTimeout(5000);
                    coon.setDoInput(true);
                    coon.setDoOutput(true);
                    coon.setUseCaches(false);
                    String data="username="+URLEncoder.encode(username,"UTF-8")+"&password="+ URLEncoder.encode(password,"UTF-8");
                    OutputStream out =coon.getOutputStream();
                    out.write(data.getBytes());
                    out.flush();
                    if(coon.getResponseCode()==200){
                        InputStream is=coon.getInputStream();
                        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
                        byte[] buffer = new byte[1024];
                        int len = 0;
                        while((len = is.read(buffer)) != -1)
                        {
                            outStream.write(buffer,0,len);
                        }
                        is.close();
                        byte[] data1 =outStream.toByteArray();;
                        String html = new String(data1, "UTF-8");
                        System.out.println(html);
                        Gson gson=new Gson();
                        LoginBean loginBean = gson.fromJson(html, LoginBean.class);
                        int  errorMsg =loginBean.getErrorCode();
                        if(errorMsg==0){
                            Message msg = new Message();
                            msg.what=1;
                            msg.obj=errorMsg;
                            handler.sendMessage(msg);

                        }else{
                            Message msg1 = new Message();
                            msg1.what=0;
                            msg1.obj=loginBean.getErrorMsg();
                            handler.sendMessage(msg1);
                        }

                    }

                    else{
                        Toast.makeText(MainActivity.this,"服务器相应失败",Toast.LENGTH_SHORT).show();
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();
    }
    public void click2(View v)
    {
        Intent intent1 = new Intent(this,regist.class);
        startActivity(intent1);
    }




    long tim;




    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("提示：");
            builder.setMessage("您确定退出？");

            //设置确定按钮
            builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    if(System.currentTimeMillis() - tim >2000){
                        System.out.println(Toast.LENGTH_LONG);
                        tim = System.currentTimeMillis();
                    }
                    else{
                        finish();
                    }
                }
            });
            //设置取消按钮
            builder.setPositiveButton("容我再想想", null);
            //显示提示框
            builder.show();
        }
        return super.onKeyDown(keyCode, event);

    }


}